<template>
  <div>
    <Category :scene="scene"></Category>

    <el-card style="margin: 10px 0px">


      <div v-show="scene == 0">
        <el-button icon="Plus" size="default" type="primary" :disabled="!categoryStore.c3Id"
          @click="addSpu">添加SPU</el-button>

        <el-table :data="records" style="margin: 10px 0px" border>
          <el-table-column label="序号" type="index" align="center" width="80" />
          <el-table-column label="SPU名称" width="280" prop="spuName" />
          <el-table-column label="SPU描述" prop="description" show-overflow-tooltip />
          <el-table-column label="操作" width="280">

            <template #default="{ row }">
              <el-button type="primary" icon="Plus" size="small" title="添加SKU" @click="addSku(row)"></el-button>
              <el-button type="primary" icon="Edit" size="small" title="修改SPU" @click="updateSpu(row)"></el-button>
              <el-button type="primary" icon="View" size="small" title="查看SKU列表" @click="findSku(row)"></el-button>
              <el-button type="primary" icon="Delete" size="small" title="删除SPU列表"></el-button>


            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[5, 50, 100, 200]"
          :background="true" layout="prev, pager, next, jumper, ->, sizes,total, " :total="total"
          @current-change="getHasSpu" @size-change="getHasSpu" />
      </div>

      <SpuForm ref="spu" v-show="scene == 1" @changeScene="changeScene"></SpuForm>

      <SkuForm ref="sku" v-show="scene == 2" @changeScene="changeScene"></SkuForm>

      <el-dialog v-model="show" title="sku列表">
<el-table :data="skuArr" border  >
  <el-table-column  label="SKU名称" prop="skuName" ></el-table-column>
  <el-table-column  label="SKU价格" prop="price" ></el-table-column>
  <el-table-column  label="SKU名重量" prop="weight" ></el-table-column>
  <el-table-column  label="SKU图片" prop="skuName" >
    <template #default="{row,$index}"> 
      <img :src="row.skuDefaultImg" style="width:100px;height:100px">
    </template>
  </el-table-column>

</el-table>

      </el-dialog>

    </el-card>


  </div>
</template>

<script setup lang="ts">

import { ref, watch } from 'vue';
import { reqHasSpu ,reqSkuList} from '@/api/product/spu';
import type { HasSpuResponseData, Records, SpuData,SkuInfoData,SkuData } from '@/api/product/spu/type';
import useCategoryStore from '@/store/modules/category';
import { ElMessage } from 'element-plus';
import SpuForm from './spuForm.vue';
import SkuForm from './skuForm.vue';
let categoryStore = useCategoryStore()
let scene = ref<number>(0);
//分页器默认的页码
let pageNo = ref<number>(1);
let pageSize = ref<number>(3);
let total = ref<number>(3);
let records = ref<Records>([]);

let spu = ref<any>()
let sku = ref<any>()
let skuArr = ref<SkuData[]>([])
let show =ref<boolean>(false)
watch(() => categoryStore.c3Id, () => {
  if (categoryStore.c3Id) {
    getHasSpu()
  }
}
)

// 获取3级分类下的SPU
const getHasSpu = async (pager: number = 1) => {
  //修改当前页面码pager=1
  if (!categoryStore.c3Id) {
    return;
  }
  pageNo.value = pager
  let result: HasSpuResponseData = await reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id)
  // console.log(result)
  if (result.code === 200) {
    total.value = result.data.total
    records.value = result.data.records
  } else {
    ElMessage.error(result.message)
  }
}

const addSpu = () => {
  scene.value = 1

  spu.value.initAddSpu(categoryStore.c3Id)
}

const updateSpu = (row: SpuData) => {
  scene.value = 1

  //调用子组件的方法
  spu.value.initHasSpuData(row)
}

const changeScene = (obj: any) => {

  scene.value = obj.flag

  // 更新spu列表
  if (obj.params == 'update') {
    getHasSpu(pageNo.value)
  } else {
    getHasSpu()
  }

}

//
const addSku = (row: SpuData) => {
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
  scene.value = 2
}

const findSku = async (row: SpuData) => {
   let result:SkuInfoData =  await reqSkuList(row.id as number )
   console.log(result)
   if (result.code === 200) {
    skuArr.value = result.data
    show.value = true
   }
  
}
</script>

<style scoped></style>
